package com.example;

import com.example.domain.User;
import com.example.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

/**
 * @author tong
 * @date 2023/01/20/12:41
 * @description
 */
public class Demo {
    public static void main(String[] args) throws Exception {
        // 定义mybatis配置文件的路径
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        // 获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        // 获取UserMapper实现类对象
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        // 调用方法测试
        List<User> users = userMapper.selectAll();
        System.out.println(users);
        // 释放资源
        sqlSession.close();
    }
}
